package org.hit.burkun.network.models;

import java.io.File;
import java.util.Collection;
import java.util.HashMap;
import java.util.HashSet;
import java.util.LinkedList;
import java.util.Queue;

import org.hit.burkun.network.EdgeInfo;
import org.hit.burkun.network.SerializableGraph;
import org.hit.burkun.network.Vertex;

public class StaticMethods {
	//private static Logger log = LoggerFactory.getLogger(StaticMethods.class);

	public static SerializableGraph loadGraphDir() {
		// 基本的统计, 78883个node，881668条边
		return (SerializableGraph)SerializableGraph.readObject(new File("data/graphDir.obj"));
	}

	public static SerializableGraph loadGraphNoDir() {
		// 基本的统计, 78883个node，881668条边
		return (SerializableGraph)SerializableGraph.readObject(new File("data/graphNoDir.obj"));
	}

	
	
	public static int counterVertexAndEdges(SerializableGraph sg){
		Collection<Vertex> vs = sg.getAllVertexs();
		int counter = 0;
		for(Vertex v : vs){
			LinkedList<EdgeInfo> ne = sg.getNeighborsByVertex(v);
			counter += ne.size();
		}
		return counter;
	}

	private static int BFSGraph(SerializableGraph sg) {
		HashSet<Vertex> maker = new HashSet<>();
		Queue<Vertex> queue = new LinkedList<Vertex>();
		Collection<Vertex> vs = sg.getAllVertexs();
		int counter = 0;
		for (Vertex v : vs) {
			if (!maker.contains(v)) {
				counter += 1;
				queue.add(v);
				maker.add(v);
				while (queue.size() != 0) {
					// 做缓存，增加遍历速度
					Vertex curID = queue.poll();
					Collection<EdgeInfo> children = sg
							.getNeighborsByVertex(curID);
					for (EdgeInfo child : children) {
						if (!maker.contains(child.getVertex())) {
							maker.add(child.getVertex());
							queue.add(child.getVertex());
						}
					}
				}
			}
		}
		return counter;
	}

	/**
	 * http://blog.jobbole.com/71431/ 
	 * 	1 A    B    C    D
	 *	2 B    A    D
	 *	3 C    C
	 *	4 D    B    C
	 *  单向
	 * @return
	 */
	public static SerializableGraph getSimpleGraph1() {
		SerializableGraph sg = new SerializableGraph();
		HashMap<String, Vertex> umls2Nodes = new HashMap<String, Vertex>();
		Vertex v1 = new Vertex();
		v1.setUmlsId("A");
		v1.addDbId("A");
		Vertex v2 = new Vertex();
		v2.setUmlsId("B");
		v2.addDbId("B");
		Vertex v3 = new Vertex();
		v3.setUmlsId("C");
		v3.addDbId("C");
		Vertex v4 = new Vertex();
		v4.setUmlsId("D");
		v4.addDbId("D");
		umls2Nodes.put("A", v1);
		umls2Nodes.put("B", v2);
		umls2Nodes.put("C", v3);
		umls2Nodes.put("D", v4);
		sg.setAllVertexs(umls2Nodes);

		// A
		sg.addEdge(v1, new EdgeInfo(v2, 2));
		sg.addEdge(v1, new EdgeInfo(v3, 3));
		sg.addEdge(v1, new EdgeInfo(v4, 1));
		// B
		sg.addEdge(v2, new EdgeInfo(v1, 4));
		sg.addEdge(v2, new EdgeInfo(v4, 1));
		// C
		sg.addEdge(v3, new EdgeInfo(v3, 1));
		// D
		sg.addEdge(v4, new EdgeInfo(v2, 1));
		sg.addEdge(v4, new EdgeInfo(v3, 6));
		return sg;
	}
	//双向,终止点问题
	public static SerializableGraph getSimpleGraph3() {
		SerializableGraph sg = new SerializableGraph();
		HashMap<String, Vertex> umls2Nodes = new HashMap<String, Vertex>();
		Vertex v1 = new Vertex();
		v1.setUmlsId("A");
		v1.addDbId("A");
		Vertex v2 = new Vertex();
		v2.setUmlsId("B");
		v2.addDbId("B");
		Vertex v3 = new Vertex();
		v3.setUmlsId("C");
		v3.addDbId("C");
		Vertex v4 = new Vertex();
		v4.setUmlsId("D");
		v4.addDbId("D");
		umls2Nodes.put("A", v1);
		umls2Nodes.put("B", v2);
		umls2Nodes.put("C", v3);
		umls2Nodes.put("D", v4);
		sg.setAllVertexs(umls2Nodes);

		// A
		sg.addEdge(v1, new EdgeInfo(v2, 1));
		sg.addEdge(v1, new EdgeInfo(v3, 3));
		sg.addEdge(v1, new EdgeInfo(v4, 1));
		// B
		sg.addEdge(v2, new EdgeInfo(v1, 8));
		sg.addEdge(v2, new EdgeInfo(v4, 1));
		// C
		sg.addEdge(v3, new EdgeInfo(v1, 1));
		sg.addEdge(v3, new EdgeInfo(v4, 2));
		//sg.addEdge(v3, new EdgeInfo(v3, 1));
		// D
		sg.addEdge(v4, new EdgeInfo(v1, 1));
		sg.addEdge(v4, new EdgeInfo(v2, 1));
		sg.addEdge(v4, new EdgeInfo(v3, 3));
		return sg;
	}
	//陷阱问题
	/**
	 * A - B
	 * B - C
	 * C - A
	 * D - A
	 * @return
	 */
	public static SerializableGraph getSimpleGraph4(){
		SerializableGraph sg = new SerializableGraph();
		HashMap<String, Vertex> umls2Nodes = new HashMap<String, Vertex>();
		Vertex v1 = new Vertex();
		v1.setUmlsId("A");
		v1.addDbId("A");
		Vertex v2 = new Vertex();
		v2.setUmlsId("B");
		v2.addDbId("B");
		Vertex v3 = new Vertex();
		v3.setUmlsId("C");
		v3.addDbId("C");
		Vertex v4 = new Vertex();
		v4.setUmlsId("D");
		v4.addDbId("D");
		umls2Nodes.put("A", v1);
		umls2Nodes.put("B", v2);
		umls2Nodes.put("C", v3);
		umls2Nodes.put("D", v4);
		sg.setAllVertexs(umls2Nodes);

		// A
		sg.addEdge(v1, new EdgeInfo(v2, 1));
		// B
		sg.addEdge(v2, new EdgeInfo(v3, 1));
		// C
		sg.addEdge(v3, new EdgeInfo(v1, 1));
		//sg.addEdge(v3, new EdgeInfo(v3, 1));
		// D
		sg.addEdge(v4, new EdgeInfo(v1, 1));
		return sg;
	}
	
	//http://www.kreny.com/pagerank_cn.htm, 不用阻尼就收敛的
	public static SerializableGraph getSimpleGraph2() {
		SerializableGraph sg = new SerializableGraph();
		HashMap<String, Vertex> umls2Nodes = new HashMap<String, Vertex>();
		Vertex v1 = new Vertex();
		v1.setUmlsId("A");
		v1.addDbId("A");
		Vertex v2 = new Vertex();
		v2.setUmlsId("B");
		v2.addDbId("B");
		Vertex v3 = new Vertex();
		v3.setUmlsId("C");
		v3.addDbId("C");
		Vertex v4 = new Vertex();
		v4.setUmlsId("D");
		v4.addDbId("D");
		Vertex v5 = new Vertex();
		v5.setUmlsId("E");
		v5.addDbId("E");
		Vertex v6 = new Vertex();
		v6.setUmlsId("F");
		v6.addDbId("F");
		Vertex v7 = new Vertex();
		v7.setUmlsId("G");
		v7.addDbId("G");

		umls2Nodes.put("A", v1);
		umls2Nodes.put("B", v2);
		umls2Nodes.put("C", v3);
		umls2Nodes.put("D", v4);
		umls2Nodes.put("E", v5);
		umls2Nodes.put("F", v6);
		umls2Nodes.put("G", v7);
		sg.setAllVertexs(umls2Nodes);

		// A
		sg.addEdge(v1, new EdgeInfo(v2, 1));
		sg.addEdge(v1, new EdgeInfo(v3, 1));
		sg.addEdge(v1, new EdgeInfo(v4, 1));
		sg.addEdge(v1, new EdgeInfo(v5, 1));
		sg.addEdge(v1, new EdgeInfo(v7, 1));
		// B
		sg.addEdge(v2, new EdgeInfo(v1, 1));
		// C
		sg.addEdge(v3, new EdgeInfo(v1, 1));
		sg.addEdge(v3, new EdgeInfo(v2, 1));
		// D
		sg.addEdge(v4, new EdgeInfo(v2, 1));
		sg.addEdge(v4, new EdgeInfo(v3, 1));
		sg.addEdge(v4, new EdgeInfo(v5, 1));
		// E
		sg.addEdge(v5, new EdgeInfo(v1, 1));
		sg.addEdge(v5, new EdgeInfo(v3, 1));
		sg.addEdge(v5, new EdgeInfo(v4, 1));
		sg.addEdge(v5, new EdgeInfo(v6, 1));
		// F
		sg.addEdge(v6, new EdgeInfo(v1, 1));
		sg.addEdge(v6, new EdgeInfo(v5, 1));
		// G
		sg.addEdge(v7, new EdgeInfo(v5, 1));
		return sg;
	}

	public static void main(String[] args) {
		
	}
}
